// pages/searchPage/searchPage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
		middleHistory:[],
		/* 搜索内容 */
		content:"",
		/* 机器人移动的位置 */
		leftLooks: 280,
		topLooks: 350,
		/* 中间数据 */
		middleHistory:[]
  },
	/**
	* 生命周期函数--监听页面加载
	*/
	onLoad: function (options) {
		if(wx.getStorageSync('middleHistory')){
			this.setData({
				middleHistory:JSON.parse(wx.getStorageSync('middleHistory'))
			})
			this.flashback();
		};
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
		
	},
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=wx.getStorageSync("phoneWidth");
		var phoneHeight=wx.getStorageSync("phoneHeight");
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
		this.setData({
			leftLooks: e.touches[0].clientX,
			topLooks: e.touches[0].clientY
		})
	},
	/* 获取搜索内容 */
	searchContent:function(e){
		this.setData({
			content:e.detail.value
		})
	},
	/* 搜索跳转到政策、解读、动态 */
	toDynamic:function(e){
		if(e.currentTarget.dataset.name){
			wx.navigateTo({
				url:"../searchDynamic/searchDynamic?content="+e.currentTarget.dataset.name
			})
		}else{
			if(this.data.content){
				this.flashback();
			}
			wx.navigateTo({
				url:"../searchDynamic/searchDynamic?content="+this.data.content
			})
		}
		
	},
	/* 历史数据倒叙排列 */
	flashback:function(){
		this.data.historyList=[];
		var len=parseInt(this.data.middleHistory.length);
		var index=-1;
		for(var i=0;i<len;i++){
			if(this.data.content==this.data.middleHistory[i].name){
				index=i;
			}
		}
		if(index!=-1){
			this.data.middleHistory.splice(index,1);
		}
		if(this.data.content){
			this.data.middleHistory.push({name:this.data.content});
		}
		var midLen=this.data.middleHistory.length-1;
		for(var j=midLen;j>=0;j--){
			this.data.historyList.push(this.data.middleHistory[j]);
		}
		this.setData({
			historyList:this.data.historyList
		})
		wx.setStorageSync('middleHistory', JSON.stringify(this.data.middleHistory));
	},
	/* 删除某一条历史记录 */
	delete:function(e){
		var _this=this;
		wx.showModal({
			title: '删除',
			content: '您确定要删除这条历史记录吗？',
			success(res) {
				if (res.confirm) {
					_this.data.historyList.splice(e.currentTarget.dataset.index,1);
					_this.setData({
						historyList:_this.data.historyList
					});
					var len=parseInt(_this.data.middleHistory.length);
					for(var i=0;i<len;i++){
						if(e.currentTarget.dataset.name==_this.data.middleHistory[i].name){
							_this.data.middleHistory.splice(i,1);
							wx.setStorageSync('middleHistory', JSON.stringify(_this.data.middleHistory));
							return;
						}
					}
				
				}
			}
		})
		
	},
	/* 清空历史数据 */
	clearAll:function(){
		var _this=this;
		wx.showModal({
			title: '清空历史记录',
			content: '您确定要清空历史记录吗？',
			success(res) {
				if (res.confirm) {
					_this.setData({
						historyList:[],
						middleHistory:[]
					})
					wx.setStorageSync("middleHistory","[]");
				}
			}
		})
		
	},
	/* 清空输入框内容 */
	clearInput:function(){
		this.setData({
			content:""
		})
	},
	/**
		 * 用户点击右上角分享朋友
		 */
		onShareAppMessage: function (e) {
			
		},
		/**
		* 用户点击右上角分享朋友圈
		*/
		onShareTimeline: function () {
	
		 },
})